﻿using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Controls;

namespace TfsComLib.FolderDiff
{
    // Microsoft.TeamFoundation.VersionControl.Controls.FolderDiff
    public sealed class CompareDialog : IDisposable
    {
        private readonly Form folderDiffDialog;

        public CompareDialog(VersionControlServer versionControlServer, ItemIdentifier item1, int version1,
            ItemIdentifier item2, int version2)
        {
            var controlsAssembly = Assembly.GetAssembly(typeof (ControlAddItemsExclude));
            var folderDiffType =
                controlsAssembly.GetType("Microsoft.TeamFoundation.VersionControl.Controls.DialogCompare");
            var ci = folderDiffType.GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new[]
                {
                    typeof (ItemIdentifier), typeof (int), typeof (ItemIdentifier), typeof (int),
                    typeof (VersionControlServer)
                },
                null);
            folderDiffDialog = (Form) ci.Invoke(new object[] {item1, version1, item2, version2, versionControlServer});
        }

        public DialogResult DialogResult { get; set; }

        #region IDisposable Members

        public void Dispose()
        {
            if (folderDiffDialog != null)
                folderDiffDialog.Dispose();
        }

        #endregion

        public void ShowDialog()
        {
            folderDiffDialog.ShowDialog();
            DialogResult = folderDiffDialog.DialogResult;
        }
    }
}